View Javadoc
1 /* 2 * Copyright (c) 2001 by 3 * Siegfried GOESCHL <mailto:siegfried.goeschl@itserv.at>; 4 * and Dima STADNIK <mailto:5d5@mail.ru>; 5 * 6 * This program is free software. 7 * 8 * You may redistribute it and/or modify it under the terms of the GNU 9 * General Public License as published by the Free Software Foundation. 10 * Version 2 of the license should be included with this distribution in 11 * the file LICENSE, as well as License.html. If the license is not 12 * included with this distribution, you may find a copy at the FSF web 13 * site at 'www.gnu.org' or 'www.fsf.org', or you may write to the 14 * Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA. 15 * 16 * THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND, 17 * NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR 18 * OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY 19 * CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR 20 * REDISTRIBUTION OF THIS SOFTWARE. 21 */ 22 23 package junit.extensions; 24 25 26 import java.io.PrintStream; 27 28 import java.util.Hashtable; 29 30 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestResult; 33 34 35 /*** 36 * A <code>PPTestResult</code> collects the results of executing 37 * a test case and implements verbose output and delaying. 38 */ 39 40 public class PPTestResult extends TestResult { 41 42 /*** used for verbose output */ 43 private PrintStream writer; 44 45 /*** delay between two tests */ 46 private int delay = 0; 47 48 /*** verbose output */ 49 private boolean verbose = false; 50 51 /*** the name of the single test to execute */ 52 private String testCaseName; 53 54 /*** mapping from thread to startime */ 55 private Hashtable threadStartMap = new Hashtable(); 56 57 public PPTestResult( 58 PrintStream writer, int delay, boolean verbose, String testCaseName ) { 59 super(); 60 this.delay = delay; 61 this.verbose = verbose; 62 this.writer = writer; 63 this.testCaseName = testCaseName; 64 } 65 66 /*** 67 * Informs the result that a test will be started. 68 */ 69 70 public void startTest(Test test) { 71 if (this.delay > 0) { 72 try { 73 Thread.currentThread().sleep(delay); 74 } catch (InterruptedException e) {} 75 } 76 if (this.verbose) { 77 Long startTime = new Long(System.currentTimeMillis()); 78 79 this.threadStartMap.put(Thread.currentThread(), startTime); 80 this.writer.println("Test started: " + getTestInfo(test)); 81 } 82 super.startTest(test); 83 } 84 85 /*** 86 * Informs the result that a test was completed. 87 */ 88 public void endTest(Test test) { 89 if (this.verbose) { 90 long endTime = System.currentTimeMillis(); 91 Long startTime = (Long) this.threadStartMap.get(Thread.currentThread()); 92 long runTime = endTime - startTime.longValue(); 93 94 this.writer.println( 95 "Test finished: " 96 + getTestInfo(test) 97 + '#' 98 + runTime 99 + " ms" 100 ); 101 } 102 } 103 104 /*** 105 * Generates verbose test info instead of the "." 106 */ 107 108 protected String getTestInfo(Test test) { 109 StringBuffer result = new StringBuffer(128); 110 111 result.append('['); 112 result.append(Thread.currentThread().getName()); 113 result.append('@'); 114 result.append(test.getClass().getName()); 115 result.append('.'); 116 result.append(((test instanceof TestCase) ? ((TestCase) test).getName() : "")); 117 result.append(']'); 118 return result.toString(); 119 } 120 121 /*** Intercept the execution of a test case and run only the desired ones */ 122 protected void run(final TestCase test) { 123 if( this.testCaseName != null ) { 124 if( this.testCaseName.equals(test.getName()) ) { 125 super.run( test ); 126 } 127 } else { 128 super.run( test ); 129 } 130 } 131 }

This page was automatically generated by Maven